<template>
  <el-card class="box-card">
    <el-table style="margin-top: 10px" border :data="messageArr">
      <el-table-column
        label="序号"
        width="80px"
        align="center"
        type="index"
      ></el-table-column>
      <el-table-column label="留言者名" align="center">
        <template #="{ row, $index }">
          <pre>{{ row.name }}</pre>
        </template>
      </el-table-column>
      <el-table-column label="留言内容" align="center">
        <template #="{ row, $index }">
          <pre>{{ row.content }}</pre>
        </template>
      </el-table-column>
      <el-table-column label="留言时间" align="center">
        <template #="{ row, $index }">
          <pre>{{ row.created_at }}</pre>
        </template>
      </el-table-column>
      <el-table-column label="留言状态" align="center">
        <template #="{ row, $index }">
          <el-tag :type="statusTagType(row.status)">
            {{ statusText(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            size="default"
            icon="Edit"
            @click="(event: MouseEvent) => updateStatus(row)"
          ></el-button>
          <el-popconfirm
            :title="`您确定要删除这条留言吗?`"
            width="200px"
            icon="Delete"
            @confirm="removeMessage(row.id)"
          >
            <template #reference>
              <el-button type="danger" size="default" icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑留言状态的对话框 -->
    <el-dialog
      v-model="dialogFormVisible"
      title="修改留言状态"
      @keydown.enter="confirm"
    >
      <el-form style="width: 80%" :model="messageParams" ref="formRef">
        <el-form-item
          label="留言状态"
          label-width="90px"
          label-position="left"
          prop="status"
        >
          <el-select v-model="messageParams.status" placeholder="请选择状态">
            <el-option label="审核中" :value="0"></el-option>
            <el-option label="审核通过" :value="1"></el-option>
            <el-option label="审核不通过" :value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" size="default" @click="confirm">
          确定
        </el-button>
        <el-button type="danger" size="default" @click="cancel">取消</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { ref, onMounted, reactive } from 'vue'
import {
  reqAdminMessageList,
  reqUpdateMessageStatus,
  reqDeleteMessage,
} from '@/api/message'
import type { Message, MessageResponseData } from '@/api/message/type'

let dialogFormVisible = ref<boolean>(false)
let messageArr = ref<Message[]>([])
let formRef = ref()

let messageParams = reactive({
  id: undefined as number | undefined,
  status: 0 as number,
})

// 获取留言列表
const getMessageList = async () => {
  let result: MessageResponseData = await reqAdminMessageList()
  if (result.code === '200') {
    messageArr.value = result.data
  }
}

// 组件挂载时获取留言列表
onMounted(() => {
  getMessageList()
})

// 状态标签类型
const statusTagType = (status: number) => {
  switch (status) {
    case 0:
      return 'warning' // 审核中
    case 1:
      return 'success' // 审核通过
    case 2:
      return 'danger' // 审核不通过
    default:
      return 'info'
  }
}

// 状态文本
const statusText = (status: number) => {
  switch (status) {
    case 0:
      return '审核中'
    case 1:
      return '审核通过'
    case 2:
      return '审核不通过'
    default:
      return '未知状态'
  }
}

// 修改留言状态
const updateStatus = (row: Message) => {
  messageParams.id = row.id
  messageParams.status = Number(row.status) || 0 // 将 row.status 转换为 number，如果为 undefined 则默认值为 0
  dialogFormVisible.value = true
}

// 删除留言
const removeMessage = async (id: number) => {
  let result: any = await reqDeleteMessage(id)
  if (result.code === '200') {
    ElMessage.success('删除成功')
    await getMessageList()
  } else {
    ElMessage.error('删除失败')
  }
}

// 确认修改状态
const confirm = async () => {
  if (messageParams.id === undefined) return

  let result: any = await reqUpdateMessageStatus(
    messageParams.id,
    messageParams.status,
  )
  if (result.code === '200') {
    ElMessage.success('状态更新成功')
    dialogFormVisible.value = false
    await getMessageList()
  } else {
    ElMessage.error('状态更新失败')
  }
}

// 取消修改状态
const cancel = () => {
  dialogFormVisible.value = false
}
</script>

<style scoped lang="scss"></style>
